import { useCloud } from '@/modules/cloud/index';
const videoCol = useCloud().database().collection('video');
export default {
    data() {
        return {
            isLiked: false,
            isCollect: false,
            videoData: {}
        }
    },
    computed: {
        likedCount() {
            if (!this.videoData.likes) {
                return 0
            }
            return this.videoData.likes.length
        }
    },
    methods: {
        // 获取目标视频
        async getTargetVideo(docId = '') {
            const res = await videoCol.doc(docId).get()

            // console.log('res', res, docId);
            if (res.data.length === 0) {
                return Promise.reject(new Error({
                    message: '未找到该视频'
                }))
            }

            let target = res.data[0]

            if (!res.data[0].views || !res.data[0].likes) {
                target = await this.initVideoData(docId)
            }

            let user = target.likes.find((user) => {
                return user.uid === this.$store.state.userInfo.uid
            })

            if (user) {
                this.isLiked = true
            }

            this.videoData = target
            console.log('获取目标视频', target, '是否点赞', this.isLiked);
            return target
        },

        // 视频初始化数据库
        async initVideoData(docId = '') {
            await videoCol.doc(docId).update({
                likes: [],
                views: []
            })
            const res2 = await videoCol.doc(docId).get()
            const video = res2.data[0]

            console.log('该视频未初始化数据库', video);
            return video
        },

        // 更新视频浏览数
        async updateVideoViews(docId = '') {
            let video = await this.getTargetVideo(docId)
            this.getCollectState(docId)


            const _id = video._id
            video.views.push({
                uid: this.$store.state.userInfo.uid || this.$reporter.getDeviceId(),
                viewTimeStamp: new Date().getTime()
            })

            const res2 = await videoCol.doc(_id).update({
                views: video.views
            })
            console.log('更新视频浏览数', res2, video);
        },

        async updateVideoLikes(docId) {

            if (this.isLiked) {
                this.$showToast('你已经点赞过了')
                return {
                    message: '你已经点赞过了'
                }
            }

            let video = await this.getTargetVideo(docId)



            video.likes.push({
                ...this.$store.state.userInfo,
                uid: this.$store.state.userInfo.uid || this.$reporter.getDeviceId(),
                likeTimeStamp: new Date().getTime()
            })

            const res2 = await videoCol.doc(video._id).update({
                likes: video.likes
            })

            this.isLiked = true
            this.$showToast('点赞成功')

            console.log('更新视频点赞数', docId, res2);
        },

        async collcetVideo(docId = '') {
            const item = await this.getTargetVideo(docId)
            const res = await this.$cloudApi.collectTarget({
                data: {
                    type: 'video',
                    itemId: item._id,
                    item,
                    path: this.$route.fullPath
                }
            })

            if (res.message) {
                this.$showToast(res.message)
            } else {

                this.$showToast('收藏成功')
            }
            console.log('收藏成功', res);
            this.isCollect = true;
            return res
        },

        async getCollectState(docId) {
            const res = await this.$cloudApi.getCollectState(docId)
            this.isCollect = res ? true : false
        }
    }
}